#include "stdafx.h"
#include "ControlGroup.h"
#include "Obj/Obj.h"
#include "Obj/PlayerObj.h"
#include "pathing/PathingManager.h"

ControlGroup::ControlGroup()
{

}

void ControlGroup::addSelection(const std::vector<PlayerObj*>& objs, bool shiftHeld)
{
    if (objs.size() == 0 ) 
        return;

    if ( shiftHeld ) {
        for ( auto it = objs.begin(); it != objs.end(); ++it ) {
            if ( std::find(m_objList.begin(), m_objList.end(), (*it)) == m_objList.end() ) {
                m_objList.push_back(*it); // only add objects which dont already exist in m_objList, TODO: consider unselecting those which are already selected
            }
        }
    } else {
        clear();
        m_objList = objs;
    }

	for ( auto it = objs.begin(); it != objs.end(); ++it ) {
		(*it)->setRenderOptions(PlayerObj::SELECTED);
	}
}

void ControlGroup::addSelection(PlayerObj* obj, bool shiftHeld)
{
    if ( !obj ) 
        return;

    std::vector<PlayerObj*> oneObjVec(1);
    oneObjVec[0] = obj;
    addSelection(oneObjVec, shiftHeld);
}

void ControlGroup::clear()
{
	for ( auto it = m_objList.begin(); it != m_objList.end(); ++it) {
		(*it)->removeRenderOption(PlayerObj::SELECTED);
	}

    m_objList.clear();
}

void ControlGroup::move(const glm::vec2& target)
{
  //  int i = 0;
  //  for ( auto it = m_objList.begin(); it != m_objList.end(); ++it )
  //  {
  //      //(*it)->setPos(target + glm::vec2(2.0f*i, 0.0f));
		//(*it)->setDest(target + glm::vec2(2.0f*i, 0.0f));
  //      ++i;

	PathingManager::getInstance()->Move(m_objList, target);
    /*}*/
}